<?php

namespace Api\Model;
class CommentModel extends CommonModel{
	
	/**
	 * 提交评论
	 * @param  string $type     类型
	 * @param  string $user_id  用户ID
	 * @param  string $other_id 其它ID，如文章ID
	 * @param  string $content  评论内容
	 * @param  string $reply_id 回复ID
	 */
	public function submit($type = 'article', $user_id = '', $other_id = '', $content = '', $reply_id = ''){
		$data['type']        = $type;
		$data['user_id']     = $user_id ? $user_id : 0;
		$data['other_id']    = $other_id ? $other_id : 0;
		$data['content']     = $content;
		$data['reply_id']    = $reply_id ? $reply_id : 0;
		$data['create_time'] = time();
		$data['ip']          = get_client_ip();
		$data['status']      = $this->get_config_val('is_audit', 'comment') == 1 ? 0 : 1;

		$this->add($data);

		return $this->getLastInsID();
	}
	public function filter_field($data){
		$data['time_desc']     = time_ago_desc($data['create_time']);
		$data['user_name'] = D('User')->get_user_name($data['user_id']);
		$data['user_head'] = D('User')->get_user_head_url($data['user_id']);
		return $data;
	}
	/**
	 * 获取评论数
	 * @param  [type] $other_id 其它ID，如文章ID
	 * @param  string $type     类型
	 */
	public function get_comment_num($other_id, $type = 'article'){
		$where['type']     = $type;
		$where['other_id'] = $other_id;
		$where['status']   = 1;
		$count = $this->where($where)->count();

		return $count ? $count : 0;
	}
}